﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Teacher
{
    public partial class MyStudent : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["DSMyStudentCommand"] = null;
                if (Request.QueryString["l_id"] != null) // 通过 QueryString 查询
                {
                    TBSearch.Text = Request.QueryString["l_id"];
                    LBSearchL_ID_Click(null, null);
                }
            }

            else
            {
                // PostBack
                if (Session["DSMyStudentCommand"] != null)
                {
                    DSMyStudent.SelectCommand = Session["DSMyStudentCommand"].ToString();
                }
            }
        }


        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            DSMyStudent.SelectCommand = "SELECT * FROM view_student_has_lesson WHERE lesson_teacher_t_id = ?";
            Session["DSMyStudentCommand"] = DSMyStudent.SelectCommand;
            GVMyStudent.EditIndex = -1;
            GVMyStudent.DataBind();
        }


        protected void LBSearchL_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int l_id = Convert.ToInt32(TBSearch.Text);
                DSMyStudent.SelectCommand =
                    $"SELECT * FROM view_student_has_lesson WHERE lesson_teacher_t_id = ? and lesson_l_id={l_id}";
                Session["DSMyStudentCommand"] = DSMyStudent.SelectCommand;
                GVMyStudent.EditIndex = -1;
                GVMyStudent.DataBind();
            }
            catch
            {
            }
        }

        protected void LBSearchS_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int s_id = Convert.ToInt32(TBSearch.Text);
                DSMyStudent.SelectCommand =
                    $"SELECT * FROM view_student_has_lesson WHERE lesson_teacher_t_id = ? and student_s_id={s_id}";
                Session["DSMyStudentCommand"] = DSMyStudent.SelectCommand;
                GVMyStudent.EditIndex = -1;
                GVMyStudent.DataBind();
            }
            catch
            {
            }
        }

        protected void LBSearchS_Name_OnClick(object sender, EventArgs e)
        {
            string s_name = TBSearch.Text;
            DSMyStudent.SelectCommand =
                $"SELECT * FROM view_student_has_lesson WHERE lesson_teacher_t_id = ? and s_name like '%{s_name}%'";
            Session["DSMyStudentCommand"] = DSMyStudent.SelectCommand;
            GVMyStudent.EditIndex = -1;
            GVMyStudent.DataBind();
        }


        protected void GVMyStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues["score"] == null)
            {
                Alert.Show("请输入合法分数", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }

            int score = Convert.ToInt32(e.NewValues["score"]);
            if (score < 0 || score > 100)
            {
                Alert.Show("请输入合法分数", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }
        }

        protected void GVMyStudent_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Alert.Show("更新成功", AlertBox.Level.Success, true);
        }

        protected void GVMyStudent_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            Alert.Show("删除成功", AlertBox.Level.Success, true);
        }
    }
}